<?php
namespace app\voice\controller;

use app\common\controller\Smscm;
use app\common\model\Users;
use app\common\model\Users as UserM;
use think\Db;
use think\Validate;
use think\Validate as UserValidate;

/**
 * 前台首页控制器
 * @package app\api\controller
 */
class Login extends Auth
{
    public $noNeedLogin = ['*'];

    public function index()
    {
        if ($this->request->isPost()) {
            $data = $this->request->post();
            $rule = [
                'phone|手机号' => 'require|regex:^1\d{10}',
                'type|类型' => 'require|number',
            ];
            // 定义验证提示
            $message = [
                'phone.regex' => '手机号不正确！',
                'phone.require' => '请输入手机号！',
                'type.require' => '请输入登录方式！',
                'type.number' => '登录方式格式不正确！',
            ];
            //$login_type = [1=>'短信登录',2=>'密码登录'];
            $validate = new Validate($rule, $message);
            if (!$validate->check($data)) {
                $this->result($validate->getError(), '', 204);
            }
            $user = UserM::where('phone', $data['phone'])->where('group',2)->find();
            if($user == null) $this->result('账户不存在', '', 204);

            if($data['type'] == 2){
                $userModel = new Users();
                switch ($userModel->checkUser($data)) {
                    case 2000;
                        Users::where(['phone'=>$data['phone']])->update(['login_token'=>token_rand()]);
                        $userInfo = Users::where('phone', $data['phone'])->find();
                        $user['avatar'] = get_img_path($user['avatar']);
                        $userInfo['store'] = Db::name('qt_store')->field('name')->where('id',$userInfo['store_id'])->find();
                        $this->result('登录成功', $userInfo, 200);
                        break;
                    case 10001;
                        $this->result('用户不存在，请先去注册', '', 205);
                        break;
                    case 10002;
                        $this->result('账户被禁用，请管理员', '', 206);
                        break;
                    case 10003;
                        $this->result('账号密码错误', '', 207);
                        break;
                }
            }
            $this->result('无效的参数', '', 204);
        } else {
            $this->result('请求错误', '', 204);
        }
    }

    /**
     * 找回密码
     */
    public function findPassword()
    {
        if ($this->request->isPost()) {
            $getData = $this->request->post();
            $validate = new UserValidate();
            if (!$validate->scene('update')->check($getData)) {
                $this->result($validate->getError(), '', 204);
            }
            $userId = UserM::where('phone', $getData['phone'])->value('id');
            if (empty($userId)) {
                $this->result('当前手机号暂未注册', '', 204);
            }
            if ($getData['password'] == $getData['new_password']) {
                $sms = new Smscm();
                $checkPhone = $sms->check_smscode($getData['phone'], $getData['code'], 2);
                if ($checkPhone['code'] == 200) {
                    $newPassword = pwd($getData['password'], $userId);
                    $update = Users::where('id', $userId)->update(['password' => $newPassword]);
                    if ($update) {
                        $this->result('密码修改成功', '', 200);
                    }
                }
                $this->result('密码修改失败', '', 204);
            }
            $this->result('两次密码不一致', '', 204);
        }
    }

}
